
//using Domain;
//using MediatR;
//using Infrastructure.Data;
//using System.Threading;
//using System.Threading.Tasks;
//using SqlSugar;
//using WriteAPI.Command.Material;

//namespace WriteAPI.CommandHandle.Material
//{
//    public class CreateMaterialReplenishmentWithDetailsHandler : IRequestHandler<CreateMaterialReplenishmentWithDetailsCommand, MaterialReplenishment>
//    {
//        private readonly ISqlSugarClient _db;

//        public CreateMaterialReplenishmentWithDetailsHandler(ISqlSugarClient db)
//        {
//            _db = db;
//        }

//        public async Task<MaterialReplenishment> Handle(CreateMaterialReplenishmentWithDetailsCommand request, CancellationToken cancellationToken)
//        {
//            // 创建主表记录
//            var material = new MaterialReplenishment
//            {
//                DocCode = Guid.NewGuid().ToString(),
//                BizType = request.BizType,
//                PlanNo = request.PlanNo,
//                DocDate = request.DocDate,
//                Operator = request.Operator,
//                SalesNo = request.SalesNo,
//                MaterialName = request.MaterialName,
//                DocStatus = request.DocStatus,
//                Auditor = request.Auditor,
//                DeliveryDate = request.DeliveryDate,
//                Unit = request.Unit,
//                BizStatus = request.BizStatus,
//                ProductionQty = request.ProductionQty,
//                Remark = request.Remark,
//                CreatedAt = DateTime.Now,
//                UpdatedAt = DateTime.Now,


//            };

//            // 使用SqlSugar事务处理
//            try
//            {


//                // 1. 保存主表记录
//                await _db.Insertable(material).ExecuteCommandAsync();


//                // 2. 保存明细表记录
//                foreach (var detail in request.MaterialDetails)
//                {

//                    detail.UpId = material.DocCode;
//                    await _db.Insertable(detail).ExecuteCommandAsync();
//                }


//                return material;
//            }
//            catch
//            {

//                throw;
//            }
//        }
//    }
//}
